<?php
/**
  * Musashi - A project manager for music groups and bands
  * Copyright (C) 2010 Daniel Torres
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  */

/**
 *  Catalog controller
 *  Shows the contents of atomic tables in the main administration view. In other
 *  words, performs simple update operations over the basic tables of our database.
 *
 *  To control the available database listings, look at the catalog model and
 *  update the sections array
 *  
 * @package Musashi
 * @author Daniel Torres
 */
 
 // Make sure we have access to this page
defined('_JEXEC') or die ('Restricted access');

/**
 *  Displays records stored on the requested table(s), but defers any update
 *  operations to other controllers.
 *  
 * @package Musashi
 * @author Daniel Torres
 */
 class MusashiAdminControllerCatalog extends JController
 {
    /**
     *  Private vars
     */
    
    
   /**
    * Constructor
    */
    function __construct()
    {
      parent::__construct();
      
      // Register the add task as part of the edit task
      $this->registerTask('add','edit');        
    }
    
   /**
    * Add or edit records on the currently selected table
    */
   function edit()
   {
      // Disable the main menu while we're editing stuff
      JRequest::setVar('hidemainmenu',1);
      
      // What are we requesting?
      $requestedSection = JRequest::getString('sectn');
      
      // Set the redirection link, and go
      JRequest::setVar('view', 'catalog');
      JRequest::setVar('layout', $requestedSection."edit" );

      parent::display();
   }
   
   /**
     *	Save method: Triggers the store/update mechanism for catalog tables
     */
    function save()
    {
      // Get the catalog model object
      $model = $this->getModel('catalog');
	
      // Attempt to save
      if($model->store()){
	       $msg = JText::_('Information updated!');
      } else {
	       $msg = $model->getError();
      }
	
      // Redirect to the previous catalog screen
      $viewSection = JRequest::getString('sectn');
      $this->setRedirect('index.php?option=com_musashi&controller=catalog&view=Catalog&sectn='.$viewSection, $msg);
    }
    
    /**
     *   Remove method: Deletes a record
     */
   function remove()
   {
      // Get the welcome model object
      $model = $this->getModel('catalog');
	
      // Attempt to delete
      if($model->delete()){
	    $msg = JText::_('Information updated!');
      } else {
	    $msg = JText::_('Error deleting record(s)');
      }
	
      // Redirect
      $viewSection = JRequest::getString('sectn');
      $this->setRedirect('index.php?option=com_musashi&controller=catalog&view=Catalog&sectn='.$viewSection, $msg);
    }


   
   /**
     *	Cancel the edit operation
     */
    function cancel()
    {
      $viewSection = JRequest::getString('sectn');
      $this->setRedirect('index.php?option=com_musashi&controller=catalog&view=Catalog&sectn='.$viewSection, 'Operation cancelled');
    }
    
    /**
     *	Adds an equipment piece into a song
     */
    function addEquipmentToSong()
    {
      // Get the welcome model object
      $model    = $this->getModel('catalog');
      $songID   = JRequest::getString('idSong');
      $quantity = JRequest::getString('quantity');
      $equID    = JRequest::getString('idEquipment');
      
      if( $model->updateEquipment( $songID, $equID, $quantity ) ){
	 $msg = "Information updated: ";
      } else {
	 $msg = "Failed to add equipment to the requested song";
      }      
      
      
      // Redirect. We know where we came from
      $viewSection = JRequest::getString('sectn');
      $this->setRedirect('index.php?option=com_musashi&controller=catalog&view=Catalog&sectn='.$viewSection, $msg);      
    }

    
    /**
     *  Displays the required content
     */
    function display()
    {
        parent::display();
    }
    
    
    
 }


?>